package com.bow.algorithm.leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

public class Solution {

	/**
	 * <pre>
	 *     给定一个整数数组 nums 和一个目标值 target，请你在该数组中找出和为目标值的那 两个 整数，并返回他们的数组下标。
	 * 你可以假设每种输入只会对应一个答案。但是，你不能重复利用这个数组中同样的元素。
	 *
	 * 示例:
	 * 给定 nums = [2, 7, 11, 15], target = 9
	 * 因为 nums[0] + nums[1] = 2 + 7 = 9
	 * 所以返回 [0, 1]
	 * </pre>
	 */
	public static int[] twoSum(int[] nums, int target) {
		for (int i = 0; i < nums.length - 1; i++) {
			for (int j = i + 1; j < nums.length; j++) {
				if (nums[i] + nums[j] == target) {
					return new int[] { i, j };
				}
			}
		}
		return null;
	}

	/**
	 * <pre>
	 *     给出一个 32 位的有符号整数，你需要将这个整数中每位上的数字进行反转。
	 * 示例 1:
	 * 输入: 123
	 * 输出: 321
	 * </pre>
	 * 
	 * @param x
	 * @return
	 */
	public static int reverse(int x) {
		char[] ary = String.valueOf(x).toCharArray();
		String v = "";
		if (x > 0) {
			for (int i = ary.length - 1; i >= 0; i--) {
				v += ary[i];
			}
		} else {
			v = "-";
			for (int i = ary.length - 1; i >= 1; i--) {
				v += ary[i];
			}
		}
		try {
			return Integer.valueOf(v);
		} catch (NumberFormatException e) {
			return 0;
		}
	}

	public static boolean isPalindrome(int x) {
		char[] ary = String.valueOf(x).toCharArray();
		for (int i = 0; i < ary.length / 2; i++) {
			if (ary[i] != ary[ary.length - 1 - i]) {
				return false;
			}
		}
		return true;
	}

	public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
		// 123 + 123

		int first = l1.val + l2.val;
		ListNode result = new ListNode(first % 10);
		if (first > 9) {
			next(l1.next, l2.next, result, true);
		} else {
			next(l1.next, l2.next, result, false);
		}
		return result;
	}

	private static void next(ListNode l1, ListNode l2, ListNode result, boolean carry) {
		if (l1 == null && l2 == null && !carry) {
			return;
		}
		int l1Val = 0;
		ListNode l1Next = null;
		if (l1 != null) {
			l1Val = l1.val;
			l1Next = l1.next;
		}

		int l2Val = 0;
		ListNode l2Next = null;
		if (l2 != null) {
			l2Val = l2.val;
			l2Next = l2.next;
		}

		int cur = l1Val + l2Val;
		if (carry) {
			cur = cur + 1;
		}
		result.next = new ListNode(cur % 10);
		if (cur > 9) {
			next(l1Next, l2Next, result.next, true);
		} else {
			next(l1Next, l2Next, result.next, false);
		}
	}

	public static class ListNode {
		int val;
		ListNode next;

		ListNode(int x) {
			val = x;
		}
	}

	public static int lengthOfLongestSubstring(String s) {
		char[] ary = s.toCharArray();
		int maxLength = 0;
		List<Character> container = new ArrayList<>();
		for(int i=0; i<ary.length; i++){

			if(container.contains(ary[i])){
				if(container.size()> maxLength){
					maxLength = container.size();
				}
				int index = container.indexOf(ary[i]);
				container = container.subList(index+1,container.size());
			}
			container.add(ary[i]);
		}
		if(container.size()>maxLength){
			maxLength = container.size();
		}
		return maxLength;
	}

	public static int lengthOfLongestSubstring1(String s) {
		char[] ary = s.toCharArray();
		int maxLength = 0;
		int begin=0;
		for(int end=1; end<ary.length; end++){
			for(int i=begin; i<end; i++){
				if(ary[i] == ary[end]){
					if(end - begin>maxLength){
						maxLength = end - begin;
					}
					begin = i+1;

				}
			}
		}
		if(ary.length - begin>maxLength){
			maxLength = ary.length - begin;
		}
		return maxLength;
	}

	public static void main(String[] args) {
		AtomicLong al = new AtomicLong(1);
		al.getAndIncrement();
		al.getAndIncrement();
		AtomicLong a2 = new AtomicLong(1);
		a2.getAndIncrement();
		a2.getAndIncrement();
	}
}
